import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { associateMemberOrderPagePost } from "@/api/tcl"
import { useEnumStore } from "@/store/enumeration"
import useInstance from '@/hooks/useInstance';
import mittBus from '@/utils/mitt';
export const orderListPageStore = defineStore('orderListPageStoreId10', () => {
    const { global } = useInstance();
    const refundComRef = ref<{ show: (type: string, row?: any) => void }>()
    const rescindOrderRef = ref<{ show: (type: string, row?: any) => void }>()
    const { orderProcessStatus, orderStatusDetails, paymentMethodList, beforeIsSign } = useEnumStore()
    const detailData = ref([
        {
            type: "text",
            prop: "supplierType",
            name: "供应商类型",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "orderListPage",
            propsList: [
                // {
                //     prop: 'supplierCode',
                //     label: '供应商编码',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                // {
                //     prop: 'supplierName',
                //     label: '供应商名称',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                // {
                //     prop: 'channelMerchantCode',
                //     label: '渠道商编码',
                //     type: 'normal',
                //     minWidth: '150px',
                //     hidden: true,
                // },
                {
                    prop: 'productCode',
                    label: '产品编码',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'productName',
                    label: '产品名称',
                    type: 'normal',
                    minWidth: '170px',
                    hidden: true,
                },
                {
                    prop: 'channelKey',
                    label: '渠道Key',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'channelName',
                    label: '渠道名称',
                    type: 'normal',
                    minWidth: '180px',
                    hidden: true,
                },
                {
                    prop: 'mobile',
                    label: '手机号',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },

                {
                    prop: 'orderNo',
                    label: '订单号',
                    type: 'normal',
                    minWidth: '220px',
                    hidden: true,
                },
                {
                    prop: 'orderTime',
                    label: '下单时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },
                {
                    prop: 'outOrderNo',
                    label: '外部订单号',
                    type: 'normal',
                    minWidth: '220px',
                    hidden: true,
                },
                {
                    prop: 'outDeductionOrderNo',
                    label: '外部支付',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },


                {
                    prop: 'signTime',
                    label: '签约时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },
                {
                    prop: 'cancelTime',
                    label: '解约时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },
                {
                    prop: 'uniqueUserId',
                    label: '唯一用户号',
                    type: 'normal',
                    minWidth: '255px',
                    hidden: true,
                },
                {
                    prop: 'alipayOrderNo',
                    label: '支付宝订单号',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'deductionAmount',
                    label: '扣款金额',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'orderStatus',
                    label: '订单状态',
                    type: 'normal',
                    minWidth: '70px',
                    hidden: true,
                },
                {
                    prop: 'orderStatusDetails',
                    label: '订单状态详情',
                    type: 'normal',
                    minWidth: '120px',
                    hidden: true,
                },
                {
                    prop: 'failedReason',
                    label: '错误原因',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },

                {
                    prop: 'beforeIsSign',
                    label: '是否签约过',
                    type: 'normal',
                    minWidth: '90px',
                    hidden: true,
                },
                {
                    prop: 'nextDeductionTime',
                    label: '下次扣款时间',
                    type: 'normal',
                    minWidth: '155px',
                    hidden: true,
                },
                {
                    prop: 'realOrderStatus',
                    label: '真实订单状态',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'paymentMethod',
                    label: '支付方式',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },

                // {
                //     prop: 'extInfo',
                //     label: '扩展信息',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '200',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearch = reactive<any>({
        orders: ["created_time desc"],
        condition: {
            supplierCode: null,//供应商编码 "TCL" 
            productCodeList: [],//产品编码
            channelCodeList: [],//渠道编码
            orderNo: null,//订单号
            mobile: null,//手机号
            orderStatus: [],// 订单状态
            orderTimeStart: null,// 订单创建时间
            outOrderNo: null,// 外部订单号
            alipayOrderNo: null,// 支付宝订单号
            orderTimeEnd: null//订单创建时间
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: [],
    })
    const operateTime = ref<any>([])

    const getData1 = async (type: string) => {

        pageParams.loadIng = true
        if (type == 'reset') {
            // operateTime.value = []
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
     
                const arr = ['supplierCode','orderStatus','orderTimeStart','orderTimeEnd']
                if (!arr.includes(it)) {
                    listSearch.condition[it] = null
                }

            })
        }
        try {
            const res = await associateMemberOrderPagePost(listSearch)
            if (res.code == 20000) {
                pageParams.tableData = res.data.records.map((item: any) => {

                    return {
                        ...item,
                        orderStatus: orderProcessStatus[item.orderStatus],
                        orderStatusDetails: orderStatusDetails[item.orderStatusDetails],
                        realOrderStatus: orderProcessStatus[item.realOrderStatus],
                        beforeIsSign: beforeIsSign[item.beforeIsSign],
                        paymentMethod: paymentMethodList[item.paymentMethod - 1]?.value,
                        // channelCode:item.channelCode + ' - ' + item.channelName
                    }
                })
                pageParams.total = res.data.total
                pageParams.loadIng = false
            }
        } catch (error) {
            pageParams.loadIng = false
            pageParams.tableData = []
            pageParams.total = 0
        }finally{
            mittBus.emit('scrollUpdate')
        }

    }
    const handPage = (page: any) => {
        console.log(page);
        listSearch.pageNum = page.pageNum
        listSearch.pageSize = page.pageSize
        getData1('search')
    }


    const timeChange = (time: any) => {
        console.log(time);
        if (time) {
            listSearch.condition.orderTimeStart = time[0]
            listSearch.condition.orderTimeEnd = time[1]
        } else {
            listSearch.condition.orderTimeStart = null
            listSearch.condition.orderTimeEnd = null
            operateTime.value = []
        }
        getData1('search')
    }
    const refund = async (row: any) => {
        refundComRef.value?.show('add', row)
    }
    const cancelContract = async (row: any) => {
        rescindOrderRef.value?.show('add', row)
    }
    return {
        operateTime, detailData, tableProp, listSearch, pageParams, refundComRef, rescindOrderRef,
        getData1, timeChange, handPage, refund, cancelContract
    }
})

